#include "..\\include\Lib_Clara.h"
#include "..\\include\Dir.h"
#include "..\\include\var_arg.h"
#include "structs.h"
#include "..\\Deskbar\skd.h"

#include <errno.h>
extern "C" long wcstol(const wchar_t *, wchar_t **, int);
void SaveSKD (void);

#define UNICODE(__x__) __x__*2+3
#define XPOS 11
#define YPOS 12
#define AFFECT_SOFTKEYS 13
#define NUMITEMS 14
#define ANIM 15
#define ANIM_SPEED 16
#define ANIM_DIR 17
#define _1ST_LINK_MODE 18
#define _2ND_LINK_MODE 19
#define _3RD_LINK_MODE 20
#define _4TH_LINK_MODE 21
#define _5TH_LINK_MODE 22
#define _6TH_LINK_MODE 23
#define _7TH_LINK_MODE 24
#define _8TH_LINK_MODE 25
#define _9TH_LINK_MODE 26
#define _10TH_LINK_MODE 27
#define VERSION 28
#define ORIENTATION 29
#define SPACING 30
#define DESK_BG 31
#define BG_X 32
#define BG_Y 33
#define S6_MODE 34
#define SEL_ICONS 35

#define DESKBAR_CHANGESKIN_EVENT 0x3014
#define DESKBAR_REINIT_EVENT 0x3012
#define DESKBAR_SKINEDIT_EDIT_MODE 0x3013

SKD * newskd;
TMP_LINKS * tmplinks;
MyBOOK *myBook;
FSTAT _fst;

int skdedited = 0;
int EditorPressed;
wchar_t *opened_fname;
wchar_t opened_fpath[256];

void RunEditor (void);

int TerminateElf(void * ,BOOK* book)
{
  FreeBook(book);
  return(1);
}

typedef struct
{
  BOOK * book;
}MSG;

int ShowAuthorInfo(void *mess ,BOOK* book)
{
  MSG * msg = (MSG*)mess;
  MessageBox(EMPTY_SID,STR("Deskbar Skin Editor\nv2.04\n(c) mc_kibel"), NOIMAGE, 1, 5000,msg->book);
  return(1);
}

int isDeskbarBook(BOOK * book)
{
  if(!strcmp(book->xbook->name,"Deskbar")) 
    return 1;
  return 0;
}

int OnEditMode (void *mess, BOOK *bk)
{
  RunEditor();
  GUIObject_Destroy( myBook->editset );
  return 0;
}

const PAGE_MSG HW_PageEvents[]@ "DYN_PAGE" ={
  DESKBAR_SKINEDIT_EDIT_MODE, OnEditMode,
  ELF_TERMINATE_EVENT , TerminateElf,
  ELF_SHOW_INFO_EVENT  , ShowAuthorInfo,
  NIL_EVENT_TAG, NULL
};
PAGE_DESC bk_base ={"HW_BasePage",0,HW_PageEvents};


void elf_exit(void)
{
  kill_data(&ELF_BEGIN, (void(*)(void*))mfree_adr());
}

static void onMyBookClose(BOOK * book)
{
  mfree (newskd);
  mfree (tmplinks);
  mfree (opened_fname);
  UI_Event(DESKBAR_REINIT_EVENT);
  SUBPROC(elf_exit);
}


void OnBackCreateTextInputGui( BOOK* bk, u16* string, int len )
{
  MyBOOK* myBook = (MyBOOK*) bk;
  GUIObject_Destroy( myBook->text_input );
}

void OnOkCreateUnicodeGui( BOOK* bk, wchar_t* string, int len )
{
  MyBOOK* myBook = (MyBOOK*) bk;
  skdedited = 1;
  if (EditorPressed == _1ST_LINK_MODE)
  {
    snwprintf (tmplinks->link1,0x1F,L"%ls",string);
    memset (newskd->link1,0,0x1F);
  }
  else if (EditorPressed == _2ND_LINK_MODE)
  {
    snwprintf (tmplinks->link2,0x1F,L"%ls",string);
    memset (newskd->link2,0,0x1F);
  }
  else if (EditorPressed == _3RD_LINK_MODE)
  {
    snwprintf (tmplinks->link3,0x1F,L"%ls",string);
    memset (newskd->link3,0,0x1F);
  }
  else if (EditorPressed == _4TH_LINK_MODE)
  {
    snwprintf (tmplinks->link4,0x1F,L"%ls",string);
    memset (newskd->link4,0,0x1F);
  }
  else if (EditorPressed == _5TH_LINK_MODE)
  {
    snwprintf (tmplinks->link5,0x1F,L"%ls",string);
    memset (newskd->link5,0,0x1F);
  }
  else if (EditorPressed == _6TH_LINK_MODE)
  {
    snwprintf (tmplinks->link6,0x1F,L"%ls",string);
    memset (newskd->link6,0,0x1F);
  }
  else if (EditorPressed == _7TH_LINK_MODE)
  {
    snwprintf (tmplinks->link7,0x1F,L"%ls",string);
    memset (newskd->link7,0,0x1F);
  }
  else if (EditorPressed == _8TH_LINK_MODE)
  {
    snwprintf (tmplinks->link8,0x1F,L"%ls",string);
    memset (newskd->link8,0,0x1F);
  }
  else if (EditorPressed == _9TH_LINK_MODE)
  {
    snwprintf (tmplinks->link9,0x1F,L"%ls",string);
    memset (newskd->link9,0,0x1F);
  }
  else if (EditorPressed == _10TH_LINK_MODE)
  {
    snwprintf (tmplinks->link10,0x1F,L"%ls",string);
    memset (newskd->link10,0,0x1F);
  }
  GUIObject_Destroy( myBook->text_input );
}

void CreateUnicodeSI( wchar_t *txt, int is_pass )
{
  wchar_t* ustr;
  int len = 63;
  STRID text, header_name; 
  ustr = new wchar_t[len + 1];
  header_name = Str2ID( L"Enter link/elf name", 0, SID_ANY_LEN );
  if (wstrlen(txt) <= 2)
    text = Str2ID( L"", 0, SID_ANY_LEN );
  else
    text = Str2ID( txt, 0, SID_ANY_LEN );
  myBook->text_input = CreateStringInputVA( 0,
    VAR_HEADER_TEXT( header_name ),
    VAR_STRINP_MIN_LEN( 1 ),
    VAR_STRINP_MAX_LEN( 63 ),
    VAR_STRINP_MODE( 1 ),
    VAR_BOOK( myBook ),
    VAR_STRINP_TEXT( text ),
    VAR_PREV_ACTION_PROC( OnBackCreateTextInputGui ),
    VAR_OK_PROC( OnOkCreateUnicodeGui ),
    VAR_STRINP_IS_PASS_MODE( is_pass ),
    0 );
  delete ustr;
}

void OnOkCreateSignedNumberGui( BOOK* bk, wchar_t* string, int len )
{
  skdedited = 1;
  MyBOOK* myBook = (MyBOOK*) bk;
  int i;
  *_Geterrno() = 0;
  i = wcstol( string, 0, 10 );
  if (EditorPressed == XPOS)
  {
    if (i > 255)
    {
      newskd->xplus = i - 255;
      newskd->xpos = 255;
    }
    else
    {
      newskd->xpos = i;
      newskd->xplus = 0;
    }
  }
  else if (EditorPressed == YPOS)
  {
    if (i > 255)
    {
      newskd->yplus = i - 255;
      newskd->ypos = 255;
    }
    else
    {
      newskd->ypos = i;
      newskd->yplus = 0;
    }
  }
  else if (EditorPressed == SPACING)
  {
    newskd->spacing = i;
  }
  else if (EditorPressed == NUMITEMS)
  {
    if (i > 10)
      MessageBox(0x6FFFFFFF,STR("Max: 10 !"),0, 1 ,3000,0);
    else
      newskd->items = i;
  }
  else if (EditorPressed == ANIM_SPEED)
  {
    if (i > 255)
      MessageBox(0x6FFFFFFF,STR("Max: 255 !"),0, 1 ,3000,0);
    else
      newskd->speed = i;
  }
  else if (EditorPressed == BG_Y)
  {
    if (i > 255)
    {
      newskd->bg_yplus = i - 255;
      newskd->bg_ypos = 255;
    }
    else
    {
      newskd->bg_ypos = i;
      newskd->bg_yplus = 0;
    }
  }
  else if (EditorPressed == BG_X)
  {
    if (i > 255)
    {
      newskd->bg_xplus = i - 255;
      newskd->bg_xpos = 255;
    }
    else
    {
      newskd->bg_xpos = i;
      newskd->bg_xplus = 0;
    }
  }
  GUIObject_Destroy (myBook->text_input);
}

void CreateSignedNumberInput( MyBOOK* myBook, wchar_t *headername, int val )
{
  STRID header_name;
  header_name = Str2ID( headername, 0, SID_ANY_LEN );
  myBook->text_input = CreateStringInputVA( 0,
    VAR_HEADER_TEXT( header_name ),
    VAR_STRINP_MODE( 2 ),
    VAR_BOOK( myBook ),
    VAR_STRINP_MIN_INT_VAL( 0 ),
    VAR_STRINP_MAX_INT_VAL( 400 ),
    VAR_STRINP_SET_INT_VAL( val ),
    VAR_PREV_ACTION_PROC( OnBackCreateTextInputGui ),
    VAR_OK_PROC( OnOkCreateSignedNumberGui ),
    0 );
}

void OnSelectDirectionSelectGui( BOOK* bk, GUI* )
{
  MyBOOK* mbk = (MyBOOK*) bk;
  skdedited = 1;
  int item = ListMenu_GetSelectedItem( mbk->direction );
  newskd->direction = item + 1;
  GUIObject_Destroy (mbk->direction);
}
    

int onSELDirLBMessage( GUI_MESSAGE* msg )
{
  MyBOOK* myBook = (MyBOOK*) GUIonMessage_GetBook( msg );
  switch( GUIonMessage_GetMsg( msg ) )
  {
  case 1:
    int item = GUIonMessage_GetCreatedItemIndex( msg );
    if (item == 0)
      GUIonMessage_SetMenuItemText( msg, STR("Right") );
    else if (item == 1)
      GUIonMessage_SetMenuItemText( msg, STR("Left") );
    else if (item == 2)
      GUIonMessage_SetMenuItemText( msg, STR("Up") );
    else if (item == 3)
      GUIonMessage_SetMenuItemText( msg, STR("Down") );
    break;
  }
  return 1;
}

void OnSelectDirectionBackGui( BOOK* bk, GUI* )
{
  MyBOOK * mbk = (MyBOOK*)bk;
  GUIObject_Destroy( mbk->direction );
}

GUI_LIST* CreateSelectDirectionGuiList( MyBOOK* bk, int set_focus )
{
  GUI_LIST* lo;
  lo = CreateListMenu( bk, 0 );
  bk->direction = lo;
  GUIObject_SetTitleText( lo, Str2ID(L"Direction",0,SID_ANY_LEN) );
  ListMenu_SetItemCount( lo, 4 );
  ListMenu_SetOnMessage( lo, onSELDirLBMessage );
  ListMenu_SetCursorToItem( lo, set_focus );
  ListMenu_SetItemStyle( lo, 0 );
  ListMenu_SetItemTextScroll( lo, 1 );
  GUIObject_SoftKeys_SetAction( lo, ACTION_BACK, OnSelectDirectionBackGui );
  GUIObject_SoftKeys_SetAction( lo, ACTION_LONG_BACK, OnSelectDirectionBackGui );
  GUIObject_SoftKeys_SetAction( lo, ACTION_SELECT1, OnSelectDirectionSelectGui );
  GUIObject_Show( lo );
  return lo;
};

void OnEnableDisableSelectGui( BOOK* bk, GUI* )
{
  MyBOOK* mbk = (MyBOOK*) bk;
  skdedited = 1;
  int item = ListMenu_GetSelectedItem( mbk->enabledisable );
  if (EditorPressed == ANIM)
    newskd->animated = item;
  else if (EditorPressed == AFFECT_SOFTKEYS)
    newskd->affect_softkeys = item;
  else if (EditorPressed == DESK_BG)
    newskd->bg_enabled = item;
  else if (EditorPressed == S6_MODE)
  {
    if (item == 1) newskd->xhack = 0xFF;
    else newskd->xhack = 0;
  }
  else if (EditorPressed == SEL_ICONS)
    newskd->sel_icon_enabled = item;
  GUIObject_Destroy (mbk->enabledisable);
}
    

int onEnableDisableLBMessage( GUI_MESSAGE* msg )
{
  MyBOOK* myBook = (MyBOOK*) GUIonMessage_GetBook( msg );
  switch( GUIonMessage_GetMsg( msg ) )
  {
  case 1:
    int item = GUIonMessage_GetCreatedItemIndex( msg );
    if (item == 0)
      GUIonMessage_SetMenuItemText( msg, STR("Disable") );
    else if (item == 1)
      GUIonMessage_SetMenuItemText( msg, STR("Enable") );
    break;
  }
  return 1;
}

void OnEnableDisableBackGui( BOOK* bk, GUI* )
{
  MyBOOK * mbk = (MyBOOK*)bk;
  GUIObject_Destroy( mbk->enabledisable );
}

GUI_LIST* CreateEnableDisableGuiList( MyBOOK* bk, int set_focus )
{
  GUI_LIST* lo;
  lo = CreateListMenu( bk, 0 );
  bk->enabledisable = lo;
  GUIObject_SetTitleText( lo, Str2ID(L"Select",0,SID_ANY_LEN) );
  ListMenu_SetItemCount( lo, 2 );
  ListMenu_SetOnMessage( lo, onEnableDisableLBMessage );
  ListMenu_SetCursorToItem( lo, set_focus );
  ListMenu_SetItemStyle( lo, 0 );
  ListMenu_SetItemTextScroll( lo, 1 );
  GUIObject_SoftKeys_SetAction( lo, ACTION_BACK, OnEnableDisableBackGui );
  GUIObject_SoftKeys_SetAction( lo, ACTION_LONG_BACK, OnEnableDisableBackGui );
  GUIObject_SoftKeys_SetAction( lo, ACTION_SELECT1, OnEnableDisableSelectGui );
  GUIObject_Show( lo );
  return lo;
};

void OnVersionSelectGui( BOOK* bk, GUI* )
{
  MyBOOK* mbk = (MyBOOK*) bk;
  skdedited = 1;
  int item = ListMenu_GetSelectedItem( mbk->version );
  newskd->skin_type = item;
  GUIObject_Destroy (mbk->version);
}
    

int onVersionLBMessage( GUI_MESSAGE* msg )
{
  MyBOOK* myBook = (MyBOOK*) GUIonMessage_GetBook( msg );
  switch( GUIonMessage_GetMsg( msg ) )
  {
  case 1:
    int item = GUIonMessage_GetCreatedItemIndex( msg );
    if (item == 0)
      GUIonMessage_SetMenuItemText( msg, STR("Old skin") );
    else if (item == 1)
      GUIonMessage_SetMenuItemText( msg, STR("New (v3.20 +)") );
    break;
  }
  return 1;
}

void OnVersionBackGui( BOOK* bk, GUI* )
{
  MyBOOK * mbk = (MyBOOK*)bk;
  GUIObject_Destroy( mbk->version );
}

GUI_LIST* CreateVersionGuiList( MyBOOK* bk, int set_focus )
{
  GUI_LIST* lo;
  lo = CreateListMenu( bk, 0 );
  bk->version = lo;
  GUIObject_SetTitleText( lo, Str2ID(L"Select",0,SID_ANY_LEN) );
  ListMenu_SetItemCount( lo, 2 );
  ListMenu_SetOnMessage( lo, onVersionLBMessage );
  ListMenu_SetCursorToItem( lo, set_focus );
  ListMenu_SetItemStyle( lo, 0 );
  ListMenu_SetItemTextScroll( lo, 1 );
  GUIObject_SoftKeys_SetAction( lo, ACTION_BACK, OnVersionBackGui );
  GUIObject_SoftKeys_SetAction( lo, ACTION_LONG_BACK, OnVersionBackGui );
  GUIObject_SoftKeys_SetAction( lo, ACTION_SELECT1, OnVersionSelectGui );
  GUIObject_Show( lo );
  return lo;
};

void OnOrientationSelectGui( BOOK* bk, GUI* )
{
  MyBOOK* mbk = (MyBOOK*) bk;
  skdedited = 1;
  int item = ListMenu_GetSelectedItem( mbk->orient );
  newskd->orientation = item;
  GUIObject_Destroy (mbk->orient);
}
    

int onOrientationLBMessage( GUI_MESSAGE* msg )
{
  MyBOOK* myBook = (MyBOOK*) GUIonMessage_GetBook( msg );
  switch( GUIonMessage_GetMsg( msg ) )
  {
  case 1:
    int item = GUIonMessage_GetCreatedItemIndex( msg );
    if (item == 0)
      GUIonMessage_SetMenuItemText( msg, STR("Horizontal") );
    else if (item == 1)
      GUIonMessage_SetMenuItemText( msg, STR("Vertical") );
    break;
  }
  return 1;
}

void OnOrientationBackGui( BOOK* bk, GUI* )
{
  MyBOOK * mbk = (MyBOOK*)bk;
  GUIObject_Destroy( mbk->orient );
}

GUI_LIST* CreateOrientationGuiList( MyBOOK* bk, int set_focus )
{
  GUI_LIST* lo;
  lo = CreateListMenu( bk, 0 );
  bk->orient = lo;
  GUIObject_SetTitleText( lo, Str2ID(L"Select",0,SID_ANY_LEN) );
  ListMenu_SetItemCount( lo, 2 );
  ListMenu_SetOnMessage( lo, onOrientationLBMessage );
  ListMenu_SetCursorToItem( lo, set_focus );
  ListMenu_SetItemStyle( lo, 0 );
  ListMenu_SetItemTextScroll( lo, 1 );
  GUIObject_SoftKeys_SetAction( lo, ACTION_BACK, OnOrientationBackGui );
  GUIObject_SoftKeys_SetAction( lo, ACTION_LONG_BACK, OnOrientationBackGui );
  GUIObject_SoftKeys_SetAction( lo, ACTION_SELECT1, OnOrientationSelectGui );
  GUIObject_Show( lo );
  return lo;
};

void elem_free( void* elem )
{
  java_list_elem* lm = (java_list_elem*) elem;
  
  if ( lm->name )
    delete( lm->name );
  
  if ( lm->fullpath )
    delete( lm->fullpath );
  
  if ( lm->imageID )
    ImageID_Free( lm->imageID );
  
  delete( lm );
}

int elem_filter( void* elem )
{
  return elem!= NULL;
}

void JavaList_Cancel_Action( BOOK* book, GUI* )
{
  MyBOOK *mbk = (MyBOOK*)book;
  if ( mbk->java_list_menu )
  {
    GUIObject_Destroy( mbk->java_list_menu );
    mbk->java_list_menu = 0;
  }
  if ( mbk->java_list )
  {
    List_DestroyElements( mbk->java_list, elem_filter, elem_free );
    List_Destroy( mbk->java_list );
    mbk->java_list = 0;
  }
}

void onEnter_JavaList( BOOK* book, GUI* )
{
  MyBOOK* mbk = (MyBOOK*) book;
  java_list_elem* elem = (java_list_elem*) List_Get( mbk->java_list, ListMenu_GetSelectedItem( mbk->java_list_menu ) );
  debug_printf ("\n -> SkinEdit: Selected AppID = %d",elem->appID);
  if (EditorPressed == _1ST_LINK_MODE)
  {
    memset (newskd->link1,0,sizeof(newskd->link1));
    memset (tmplinks->link1,0,sizeof(tmplinks->link1));
    newskd->link1[0] = elem->appID;
    tmplinks->link1[0] = elem->appID;
  }
  else if (EditorPressed == _2ND_LINK_MODE)
  {
    memset (newskd->link2,0,sizeof(newskd->link2));
    memset (tmplinks->link2,0,sizeof(tmplinks->link2));
    newskd->link2[0] = elem->appID;
    tmplinks->link2[0] = elem->appID;
  }
  else if (EditorPressed == _3RD_LINK_MODE)
  {
    memset (newskd->link3,0,sizeof(newskd->link3));
    memset (tmplinks->link3,0,sizeof(tmplinks->link3));
    newskd->link3[0] = elem->appID;
    tmplinks->link3[0] = elem->appID;
  }
  else if (EditorPressed == _4TH_LINK_MODE)
  {
    memset (newskd->link4,0,sizeof(newskd->link4));
    memset (tmplinks->link4,0,sizeof(tmplinks->link4));
    newskd->link4[0] = elem->appID;
    tmplinks->link4[0] = elem->appID;
  }
  else if (EditorPressed == _5TH_LINK_MODE)
  {
    memset (newskd->link5,0,sizeof(newskd->link5));
    memset (tmplinks->link5,0,sizeof(tmplinks->link5));
    newskd->link5[0] = elem->appID;
    tmplinks->link5[0] = elem->appID;
  }
  else if (EditorPressed == _6TH_LINK_MODE)
  {
    memset (newskd->link6,0,sizeof(newskd->link6));
    memset (tmplinks->link6,0,sizeof(tmplinks->link6));
    newskd->link6[0] = elem->appID;
    tmplinks->link6[0] = elem->appID;
  }
  else if (EditorPressed == _7TH_LINK_MODE)
  {
    memset (newskd->link7,0,sizeof(newskd->link7));
    memset (tmplinks->link7,0,sizeof(tmplinks->link7));
    newskd->link7[0] = elem->appID;
    tmplinks->link7[0] = elem->appID;
  }
  else if (EditorPressed == _8TH_LINK_MODE)
  {
    memset (newskd->link8,0,sizeof(newskd->link8));
    memset (tmplinks->link8,0,sizeof(tmplinks->link8));
    newskd->link8[0] = elem->appID;
    tmplinks->link8[0] = elem->appID;
  }
  else if (EditorPressed == _9TH_LINK_MODE)
  {
    memset (newskd->link9,0,sizeof(newskd->link9));
    memset (tmplinks->link9,0,sizeof(tmplinks->link9));
    newskd->link9[0] = elem->appID;
    tmplinks->link9[0] = elem->appID;
  }
  else if (EditorPressed == _10TH_LINK_MODE)
  {
    memset (newskd->link10,0,sizeof(newskd->link10));
    memset (tmplinks->link10,0,sizeof(tmplinks->link10));
    newskd->link10[0] = elem->appID;
    tmplinks->link10[0] = elem->appID;
  }
  JavaList_Cancel_Action( myBook, 0 );
}

int java_list_callback( GUI_MESSAGE* msg )
{
  MyBOOK* mbk = (MyBOOK*) GUIonMessage_GetBook( msg );
  
  switch( GUIonMessage_GetMsg( msg ) )
  {
  case LISTMSG_GetItem:
    java_list_elem* elem = (java_list_elem*) List_Get( mbk->java_list, GUIonMessage_GetCreatedItemIndex( msg ) );
    GUIonMessage_SetMenuItemText( msg, Str2ID( elem->name, 0, SID_ANY_LEN ) );
    JavaApp_LogoImageID_Get( elem->fullpath, &elem->imageID );
    GUIonMessage_SetMenuItemIcon( msg, 0, elem->imageID );
  }
  return 1;
}


java_list_elem* CreateElem( void* JavaDesc )
{
  java_list_elem* elem = new java_list_elem;
  wchar_t* sp;
  JavaAppDesc_GetJavaAppInfo( JavaDesc, 0, &sp );
  elem->name = sp;
  JavaAppDesc_GetJavaAppInfo( JavaDesc, 5, &sp );
  elem->fullpath = sp;
  elem->appID = JavaAppDesc_GetJavaAppID( JavaDesc );
  return elem;
}


int CreateJavaList( void* data, BOOK* book )
{
  MyBOOK* mbk = (MyBOOK*) book;

  if ( mbk->java_list )
  {
    List_DestroyElements( mbk->java_list, elem_filter, elem_free );
    List_Destroy( mbk->java_list );
  }

  mbk->java_list = List_Create();
  char sp1;
  void* JavaDesc;
  JavaDialog_Open( 0, &sp1, &JavaDesc );

  if ( !JavaAppDesc_GetFirstApp( JavaDesc ) )
  {
    int result = 0;
    while ( !result )
    {
      List_InsertLast( mbk->java_list, CreateElem( JavaDesc ) );
      result = JavaAppDesc_GetNextApp( JavaDesc );
    }
  }

  JavaDialog_Close( sp1 );

  if ( mbk->java_list_menu )
    GUIObject_Destroy( mbk->java_list_menu );

  mbk->java_list_menu = CreateListMenu( book, 0 );
  int str_id;
  textidname2id(L"DB_APPLICATIONS_TXT",SID_ANY_LEN,&str_id); // "Applications"
  GUIObject_SetTitleText( mbk->java_list_menu, str_id );
  ListMenu_SetItemCount( mbk->java_list_menu, mbk->java_list->FirstFree );
  ListMenu_SetOnMessage( mbk->java_list_menu, java_list_callback );
  ListMenu_SetCursorToItem( mbk->java_list_menu, 0 );
  GUIObject_SoftKeys_SetAction( mbk->java_list_menu, ACTION_BACK, JavaList_Cancel_Action );
  GUIObject_SoftKeys_SetAction( mbk->java_list_menu, ACTION_LONG_BACK, JavaList_Cancel_Action );
  GUIObject_SoftKeys_SetAction( mbk->java_list_menu, ACTION_SELECT1, onEnter_JavaList );
  GUIObject_Show( mbk->java_list_menu );
  return 0;
}

void OnEditorSelectGui( BOOK* bk, GUI* )
{
  MyBOOK* mbk = (MyBOOK*) bk;
  int item = ListMenu_GetSelectedItem( mbk->editor );
  int animdir_pos;
  switch (item)
  {
  case 0:
    EditorPressed = XPOS;
    CreateSignedNumberInput( myBook, L"X position", newskd->xpos + newskd->xplus );
    break;
  case 1:
    EditorPressed = YPOS;
    CreateSignedNumberInput( myBook, L"Y position", newskd->ypos + newskd->yplus );
    break;
  case 2:
    EditorPressed = VERSION;
    CreateVersionGuiList( myBook, newskd->skin_type );
    break;
  case 3:
    EditorPressed = ORIENTATION;
    CreateOrientationGuiList( myBook, newskd->orientation );
    break;
  case 4:
    EditorPressed = SPACING;
    CreateSignedNumberInput( myBook, L"Spacing", newskd->spacing );
    break;
  case 5:
    EditorPressed = ANIM;
    CreateEnableDisableGuiList( myBook, newskd->animated );
    break;
  case 6:
    EditorPressed = ANIM_DIR;
    if (newskd->direction == 0)
      animdir_pos = 0;
    else
      animdir_pos = newskd->direction - 1;
    CreateSelectDirectionGuiList( myBook, animdir_pos );
    break;
  case 7:
    EditorPressed = ANIM_SPEED;
    CreateSignedNumberInput( myBook, L"Show/hide speed", newskd->speed );
    break;
  case 8:
    EditorPressed = AFFECT_SOFTKEYS;
    CreateEnableDisableGuiList( myBook, newskd->affect_softkeys );
    break;
  case 9:
    EditorPressed = NUMITEMS;
    CreateSignedNumberInput( myBook, L"Items", newskd->items);
    break;
  case 10:
    EditorPressed = DESK_BG;
    CreateEnableDisableGuiList( myBook, newskd->bg_enabled );
    break;
  case 11:
    EditorPressed = BG_X;
    CreateSignedNumberInput( myBook, L"Background X", 
                            _fst.fsize == 336 ? 0 : newskd->bg_xpos + newskd->bg_xplus);
    break;
  case 12:
    EditorPressed = BG_Y;
    CreateSignedNumberInput( myBook, L"Background Y", 
                            _fst.fsize == 336 ? 0 : newskd->bg_ypos + newskd->bg_yplus);
    break;
  case 13:
    EditorPressed = S6_MODE;
    CreateEnableDisableGuiList( myBook, newskd->xhack == 0xFF ? 1 : 0 );
    break;
  case 14:
    EditorPressed = SEL_ICONS;
    CreateEnableDisableGuiList( myBook, newskd->sel_icon_enabled );
    break;
  }
}

int onEditorLBMessage( GUI_MESSAGE* msg )
{
  MyBOOK* myBook = (MyBOOK*) GUIonMessage_GetBook( msg );
  switch( GUIonMessage_GetMsg( msg ) )
  {
  case 1:
    int item = GUIonMessage_GetCreatedItemIndex( msg );
    if (item == 0)
      GUIonMessage_SetMenuItemText( msg, STR("X position") );
    else if (item == 1)
      GUIonMessage_SetMenuItemText( msg, STR("Y position") );
    else if (item == 2)
      GUIonMessage_SetMenuItemText( msg, STR("Skin version") );
    else if (item == 3)
      GUIonMessage_SetMenuItemText( msg, STR("Orientation") );
    else if (item == 4)
      GUIonMessage_SetMenuItemText( msg, STR("Space between icons") );
    else if (item == 5)
      GUIonMessage_SetMenuItemText( msg, STR("Show/hide animation") );
    else if (item == 6)
      GUIonMessage_SetMenuItemText( msg, STR("Show/hide direction") );
    else if (item == 7)
      GUIonMessage_SetMenuItemText( msg, STR("Animation speed") );
    else if (item == 8)
      GUIonMessage_SetMenuItemText( msg, STR("Affect softkeys") );
    else if (item == 9)
      GUIonMessage_SetMenuItemText( msg, STR("Num. of items") );
    else if (item == 10)
      GUIonMessage_SetMenuItemText( msg, STR("Deskbar background") );
    else if (item == 11)
      GUIonMessage_SetMenuItemText( msg, STR("Background X") );
    else if (item == 12)
      GUIonMessage_SetMenuItemText( msg, STR("Background Y") );
    else if (item == 13)
      GUIonMessage_SetMenuItemText( msg, STR("Smooth-6 mode") );
    else if (item == 14)
      GUIonMessage_SetMenuItemText( msg, STR("Selected icons") );
  }
  return 1;
}

void OnNoExitGui(BOOK * bk, void *)
{
  FreeBook(bk);
}

void OnYesTurn(BOOK * bk, void *)
{
  SaveSKD();
  FreeBook(bk);
}

void SaveSKD (void)
{
  newskd->hdr[0] = 'S';
  newskd->hdr[1] = 'K';
  newskd->hdr[2] = 'D';
  if (newskd->l1mode != LINK_APP) wstr2strn (newskd->link1,tmplinks->link1,31);
  if (newskd->l1mode != LINK_APP) wstr2strn (newskd->link2,tmplinks->link2,31);
  if (newskd->l1mode != LINK_APP) wstr2strn (newskd->link3,tmplinks->link3,31);
  if (newskd->l1mode != LINK_APP) wstr2strn (newskd->link4,tmplinks->link4,31);
  if (newskd->l1mode != LINK_APP) wstr2strn (newskd->link5,tmplinks->link5,31);
  if (newskd->l1mode != LINK_APP) wstr2strn (newskd->link6,tmplinks->link6,31);
  if (newskd->l1mode != LINK_APP) wstr2strn (newskd->link7,tmplinks->link7,31);
  if (newskd->l1mode != LINK_APP) wstr2strn (newskd->link8,tmplinks->link8,31);
  if (newskd->l1mode != LINK_APP) wstr2strn (newskd->link9,tmplinks->link9,31);
  if (newskd->l1mode != LINK_APP) wstr2strn (newskd->link10,tmplinks->link10,31);
  int to_save = _fopen(opened_fpath,opened_fname,WA_Write,0x180,0);
  fwrite( to_save, newskd, sizeof(SKD) );  
  fclose (to_save);
}

void CreateTurnQuestion(void)
{
  int changes_have_been_made;
  int save_before_exit;
  textidname2id( L"HPB_SAVE_BEFORE_EXITD_TXT", - 1, &changes_have_been_made );
  textidname2id( L"HPB_SAVE_BEFORE_EXITQ_TXT", - 1, &save_before_exit );
  myBook->yesno = CreateYesNoQuestionVA(0,
                                VAR_BOOK( myBook ),
                                VAR_YESNO_PRE_QUESTION( changes_have_been_made ),
                                VAR_YESNO_QUESTION( save_before_exit ),
                                VAR_YESNO_YES_ACTION( OnYesTurn ),
                                VAR_YESNO_NO_ACTION( OnNoExitGui ),
                                0);
}

void OnEditorBackGui( BOOK* bk, GUI* )
{
  if (skdedited == 1)
    CreateTurnQuestion();
  else
    FreeBook(bk);
}

GUI_LIST* CreateEditorGui( MyBOOK* bk, int set_focus )
{
  GUI_LIST* lo;
  lo = CreateListMenu( bk, 0 );
  bk->editor = lo;
  ListMenu_SetItemCount( lo, 15 );
  ListMenu_SetOnMessage( lo, onEditorLBMessage );
  ListMenu_SetCursorToItem( lo, set_focus );
  ListMenu_SetItemStyle( lo, 0 );
  ListMenu_SetItemTextScroll( lo, 1 );
  GUIObject_SoftKeys_SetAction( lo, ACTION_BACK, OnEditorBackGui );
  GUIObject_SoftKeys_SetAction( lo, ACTION_LONG_BACK, OnEditorBackGui );
  GUIObject_SoftKeys_SetAction( lo, ACTION_SELECT1, OnEditorSelectGui );
  return lo;
};

int IconIDname2ID (wchar_t *idname)
{
  int nIconID;
  iconidname2id( idname, -1, &nIconID );
  return nIconID;
}

GUI_TABMENUBAR *CreateTabGui (MyBOOK *bk,int selected_tab)
{
  GUI_TABMENUBAR *tb;
  tb = CreateTabMenuBar( bk );
  TabMenuBar_SetTabCount( tb, 2 );
  int iconID_1 = IconIDname2ID(L"RN_SHORTCUT_LIST_ENTERTAINMENT_ICN");
  int iconID_2 = IconIDname2ID(L"RN_SHORTCUT_LIST_MENU_ICN");
  TabMenuBar_SetTabIcon( tb, 0, iconID_1, 0 );
  TabMenuBar_SetTabIcon( tb, 0, iconID_1, 1 );
  TabMenuBar_SetTabIcon( tb, 1, iconID_2, 0 );
  TabMenuBar_SetTabIcon( tb, 1, iconID_2, 1 );
  TabMenuBar_SetTabTitle( tb, 0, STR( "Editor: General" ) );
  TabMenuBar_SetTabTitle( tb, 1, STR( "Editor: Links" ) );
  TabMenuBar_SetTabGui( tb, 0, bk->editor );
  TabMenuBar_SetTabGui( tb, 1, bk->editor_links );
  TabMenuBar_SetFocusedTab( tb, selected_tab );
  GUIObject_Show(tb);
  return tb;
}

void OnLinkModeSelectGui( BOOK* bk, GUI* )
{
  MyBOOK* mbk = (MyBOOK*) bk;
  skdedited = 1;
  int item = ListMenu_GetSelectedItem( mbk->link_mode );
  if (EditorPressed == _1ST_LINK_MODE)
  {
    newskd->l1mode = item;
    if (newskd->l1mode == LINK_APP)
      CreateJavaList( 0, myBook );
    else
      CreateUnicodeSI( tmplinks->link1, 0 );
  }
  else if (EditorPressed == _2ND_LINK_MODE)
  {
    newskd->l2mode = item;
    if (newskd->l2mode == LINK_APP)
      CreateJavaList( 0, myBook );
    else
      CreateUnicodeSI( tmplinks->link2, 0 );
  }
  else if (EditorPressed == _3RD_LINK_MODE)
  {
    newskd->l3mode = item;
    if (newskd->l3mode == LINK_APP)
      CreateJavaList( 0, myBook );
    else
      CreateUnicodeSI( tmplinks->link3, 0 );
  }
  else if (EditorPressed == _4TH_LINK_MODE)
  {
    newskd->l4mode = item;
    if (newskd->l4mode == LINK_APP)
      CreateJavaList( 0, myBook );
    else
      CreateUnicodeSI( tmplinks->link4, 0 );
  }
  else if (EditorPressed == _5TH_LINK_MODE)
  {
    newskd->l5mode = item;
    if (newskd->l5mode == LINK_APP)
      CreateJavaList( 0, myBook );
    else
      CreateUnicodeSI( tmplinks->link5, 0 );
  }
  else if (EditorPressed == _6TH_LINK_MODE)
  {
    newskd->l6mode = item;
    if (newskd->l6mode == LINK_APP)
      CreateJavaList( 0, myBook );
    else
      CreateUnicodeSI( tmplinks->link6, 0 );
  }
  else if (EditorPressed == _7TH_LINK_MODE)
  {
    newskd->l7mode = item;
    if (newskd->l7mode == LINK_APP)
      CreateJavaList( 0, myBook );
    else
      CreateUnicodeSI( tmplinks->link7, 0 );
  }
  else if (EditorPressed == _8TH_LINK_MODE)
  {
    newskd->l8mode = item;
    if (newskd->l8mode == LINK_APP)
      CreateJavaList( 0, myBook );
    else
      CreateUnicodeSI( tmplinks->link8, 0 );
  }
  else if (EditorPressed == _9TH_LINK_MODE)
  {
    newskd->l9mode = item;
    if (newskd->l9mode == LINK_APP)
      CreateJavaList( 0, myBook );
    else
      CreateUnicodeSI( tmplinks->link9, 0 );
  }
  else if (EditorPressed == _10TH_LINK_MODE)
  {
    newskd->l10mode = item;
    if (newskd->l10mode == LINK_APP)
      CreateJavaList( 0, myBook );
    else
      CreateUnicodeSI( tmplinks->link10, 0 );
  }
  GUIObject_Destroy (mbk->link_mode);
}

int onLinkModeLBMessage( GUI_MESSAGE* msg )
{
  MyBOOK* myBook = (MyBOOK*) GUIonMessage_GetBook( msg );
  switch( GUIonMessage_GetMsg( msg ) )
  {
  case 1:
    int item = GUIonMessage_GetCreatedItemIndex( msg );
    if (item == 0)
      GUIonMessage_SetMenuItemText( msg, STR("Menu") );
    else if (item == 1)
      GUIonMessage_SetMenuItemText( msg, STR("Java") );
    else if (item == 2)
      GUIonMessage_SetMenuItemText( msg, STR("Elf") );
    break;
  }
  return 1;
}

void OnLinkModeBackGui( BOOK* bk, GUI* )
{
  MyBOOK * mbk = (MyBOOK*)bk;
  GUIObject_Destroy( mbk->link_mode );
}

GUI_LIST* CreateLinkModeGui( MyBOOK* bk, int set_focus )
{
  GUI_LIST* lo;
  lo = CreateListMenu( bk, 0 );
  bk->link_mode = lo;
  GUIObject_SetTitleText( lo, Str2ID(L"Link mode",0,SID_ANY_LEN) );
  ListMenu_SetItemCount( lo, 3 );
  ListMenu_SetOnMessage( lo, onLinkModeLBMessage );
  ListMenu_SetCursorToItem( lo, set_focus );
  ListMenu_SetItemStyle( lo, 0 );
  ListMenu_SetItemTextScroll( lo, 1 );
  GUIObject_SoftKeys_SetAction( lo, ACTION_BACK, OnLinkModeBackGui );
  GUIObject_SoftKeys_SetAction( lo, ACTION_LONG_BACK, OnLinkModeBackGui );
  GUIObject_SoftKeys_SetAction( lo, ACTION_SELECT1, OnLinkModeSelectGui );
  GUIObject_Show( lo );
  return lo;
};

void OnLinksEditorSelectGui( BOOK* bk, GUI* )
{
  MyBOOK* mbk = (MyBOOK*) bk;
  int item = ListMenu_GetSelectedItem( mbk->editor_links );
  if (item == 0)
  {
    EditorPressed = _1ST_LINK_MODE;
    CreateLinkModeGui(myBook,newskd->l1mode);
  }
  else if (item == 1)
  {
    EditorPressed = _2ND_LINK_MODE;
    CreateLinkModeGui(myBook,newskd->l2mode);
  }
  else if (item == 2)
  {
    EditorPressed = _3RD_LINK_MODE;
    CreateLinkModeGui(myBook,newskd->l3mode);
  }
  else if (item == 3)
  {
    EditorPressed = _4TH_LINK_MODE;
    CreateLinkModeGui(myBook,newskd->l4mode);
  }
  else if (item == 4)
  {
    EditorPressed = _5TH_LINK_MODE;
    CreateLinkModeGui(myBook,newskd->l5mode);
  }
  else if (item == 5)
  {
    EditorPressed = _6TH_LINK_MODE;
    CreateLinkModeGui(myBook,newskd->l6mode);
  }
  else if (item == 6)
  {
    EditorPressed = _7TH_LINK_MODE;
    CreateLinkModeGui(myBook,newskd->l7mode);
  }
  else if (item == 7)
  {
    EditorPressed = _8TH_LINK_MODE;
    CreateLinkModeGui(myBook,newskd->l8mode);
  }
  else if (item == 8)
  {
    EditorPressed = _9TH_LINK_MODE;
    CreateLinkModeGui(myBook,newskd->l9mode);
  }
  else if (item == 9)
  {
    EditorPressed = _10TH_LINK_MODE;
    CreateLinkModeGui(myBook,newskd->l10mode);
  }
}

int onLinksLBMessage( GUI_MESSAGE* msg )
{
  MyBOOK* myBook = (MyBOOK*) GUIonMessage_GetBook( msg );
  switch( GUIonMessage_GetMsg( msg ) )
  {
  case 1:
    int item = GUIonMessage_GetCreatedItemIndex( msg );
    if (item == 0)
      GUIonMessage_SetMenuItemText( msg, STR("1st link") );
    else if (item == 1)
      GUIonMessage_SetMenuItemText( msg, STR("2nd link") );
    else if (item == 2)
      GUIonMessage_SetMenuItemText( msg, STR("3rd link") );
    else if (item == 3)
      GUIonMessage_SetMenuItemText( msg, STR("4th link") );
    else if (item == 4)
      GUIonMessage_SetMenuItemText( msg, STR("5th link") );
    else if (item == 5)
      GUIonMessage_SetMenuItemText( msg, STR("6th link") );
    else if (item == 6)
      GUIonMessage_SetMenuItemText( msg, STR("7th link") );
    else if (item == 7)
      GUIonMessage_SetMenuItemText( msg, STR("8th link") );
    else if (item == 8)
      GUIonMessage_SetMenuItemText( msg, STR("9th link") );
    else if (item == 9)
      GUIonMessage_SetMenuItemText( msg, STR("10th link") );
  }
  return 1;
}

GUI_LIST* CreateLinksEditorGui( MyBOOK* bk, int set_focus )
{
  GUI_LIST* lo;
  lo = CreateListMenu( bk, 0 );
  bk->editor_links = lo;
  ListMenu_SetItemCount( lo, 10 );
  ListMenu_SetOnMessage( lo, onLinksLBMessage );
  ListMenu_SetCursorToItem( lo, set_focus );
  ListMenu_SetItemStyle( lo, 0 );
  ListMenu_SetItemTextScroll( lo, 1 );
  GUIObject_SoftKeys_SetAction( lo, ACTION_BACK, OnEditorBackGui );
  GUIObject_SoftKeys_SetAction( lo, ACTION_LONG_BACK, OnEditorBackGui );
  GUIObject_SoftKeys_SetAction( lo, ACTION_SELECT1, OnLinksEditorSelectGui );
  return lo;
};
  
void RunEditor (void)
{
  newskd = (SKD*)malloc(sizeof(SKD));
  memset (newskd,0,sizeof(SKD));
  int f2edit = _fopen(opened_fpath,opened_fname,0x001,0x180,0);
  fstat(opened_fpath,opened_fname,&_fst);
  fread  (f2edit, newskd, sizeof(SKD));
  fclose (f2edit);
  tmplinks = (TMP_LINKS*)malloc(sizeof(TMP_LINKS));
  str2wstr (tmplinks->link1,newskd->link1);
  str2wstr (tmplinks->link2,newskd->link2);
  str2wstr (tmplinks->link3,newskd->link3);
  str2wstr (tmplinks->link4,newskd->link4);
  str2wstr (tmplinks->link5,newskd->link5);
  str2wstr (tmplinks->link6,newskd->link6);
  str2wstr (tmplinks->link7,newskd->link7);
  str2wstr (tmplinks->link8,newskd->link8);
  str2wstr (tmplinks->link9,newskd->link9);
  str2wstr (tmplinks->link10,newskd->link10);
  CreateEditorGui(myBook, 0);
  CreateLinksEditorGui(myBook, 0);
  CreateTabGui (myBook,0);
}

void OnEditSetSelectGui( BOOK* bk, GUI* )
{
  int item = ListMenu_GetSelectedItem( myBook->editset );
  if (item == 0)
  {
    if (FindBook(isDeskbarBook) == 0)
    {
      MessageBox(0x6FFFFFFF,STR("Deskbar must be started!"),0, 1 ,3000,0);
      return;
    }
    UI_Event_wData( DESKBAR_CHANGESKIN_EVENT, opened_fpath, 0 );
    FreeBook(myBook);
  }
  else if (item == 1)
    RunEditor();
}
    

int onEditSetLBMessage( GUI_MESSAGE* msg )
{
  MyBOOK* myBook = (MyBOOK*) GUIonMessage_GetBook( msg );
  switch( GUIonMessage_GetMsg( msg ) )
  {
  case 1:
    int item = GUIonMessage_GetCreatedItemIndex( msg );
    if (item == 0)
    {
      GUIonMessage_SetMenuItemIcon( msg, 0, IconIDname2ID(L"RN_SHORTCUT_LIST_ENTERTAINMENT_ICN") );
      GUIonMessage_SetMenuItemText( msg, STR("Set skin") );
    }
    else if (item == 1)
    {
      GUIonMessage_SetMenuItemIcon( msg, 0, IconIDname2ID(L"RN_SHORTCUT_LIST_MENU_ICN") );
      GUIonMessage_SetMenuItemText( msg, STR("Edit skin") );
    }
    break;
  }
  return 1;
}

void OnEditSetBackGui( BOOK* bk, GUI* )
{
  BookObj_Hide(myBook,0);
  GUIObject_Destroy( myBook->editset );
  FreeBook(bk);
}

GUI_LIST* CreateEditSetGuiList( MyBOOK* bk, int set_focus )
{
  GUI_LIST* lo;
  lo = CreateListMenu( bk, 0 );
  bk->editset = lo;
  GUIObject_SetTitleText( lo, Str2ID(L"Select",0,SID_ANY_LEN) );
  ListMenu_SetItemCount( lo, 2 );
  ListMenu_SetOnMessage( lo, onEditSetLBMessage );
  ListMenu_SetCursorToItem( lo, set_focus );
  ListMenu_SetItemStyle( lo, 0 );
  ListMenu_SetItemTextScroll( lo, 1 );
  GUIObject_SoftKeys_SetAction( lo, ACTION_BACK, OnEditSetBackGui );
  GUIObject_SoftKeys_SetAction( lo, ACTION_LONG_BACK, OnEditSetBackGui );
  GUIObject_SoftKeys_SetAction( lo, ACTION_SELECT1, OnEditSetSelectGui );
  GUIObject_Show( lo );
  return lo;
};

int main (wchar_t *elfname, wchar_t *fpath, wchar_t *fname)
{
  if (!fname || !fpath)
  {
    MessageBox(0x6FFFFFFF,STR("Click on .skd file to edit/set it."),0, 1 ,3000,0);
    elf_exit();
    return 1;
  }
  myBook = new MyBOOK;
  memset( myBook, 0, sizeof( MyBOOK ) );
  CreateBook(myBook,onMyBookClose,&bk_base,"SkinEditor",-1,0);
  int fname_len = wstrlen (fname);
  opened_fname = (wchar_t*)malloc(UNICODE(fname_len));
  memset (opened_fname,0,UNICODE(fname_len));
  wstrcpy (opened_fname,fname);
  wstrcpy (opened_fpath,fpath);
  CreateEditSetGuiList( myBook, 0 );
  return 0;
}
